<template>
  <div class="login-container">
    <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form" auto-complete="on" label-position="left">

      <div class="title-container">
        <el-image
          style="width: 100px; height: 100px"
          src="http://likede2-admin.itheima.net/img/logo.595745bd.png"
          fit="contain"
        />
      </div>

      <el-form-item prop="loginName">
        <span class="svg-container">
          <span class="el-icon-mobile-phone" />
        </span>
        <el-input
          ref="loginName"
          v-model="loginForm.loginName"
          placeholder="请输入用户名"
        />
      </el-form-item>

      <el-form-item prop="password">
        <span class="svg-container">
          <span class="el-icon-lock" />
        </span>
        <el-input
          :key="passwordType"
          ref="password"
          v-model="loginForm.password"
          :type="passwordType"
          placeholder="Password"
          @keyup="handleLogin"
        />
        <span class="show-pwd" @click="showPwd">
          <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" />
        </span>
      </el-form-item>

      <div class="tab">
        <el-form-item prop="code" class="validate">
          <span class="svg-container">
            <span class="el-icon-success" />
          </span>
          <el-input
            ref="code"
            v-model="loginForm.code"
            placeholder="请输入验证码"
          />
        </el-form-item>
        <el-image
          style="width: 130px; height: 50px"
          :src="photoUrl + loginForm.clientToken"
          fit="contain"
          class="img"
          @click="validateLogin"
        />
      </div>

      <el-button v-loading="loading" type="primary" style="width:100%;height: 52px;background: linear-gradient(262deg,#2e50e1,#6878f0);" @click="handleLogin">登录</el-button>

    </el-form>
  </div>
</template>

<script>
import { imgSrc } from '@/utils/request'
export default {
  name: 'Login',
  data() {
    const validateLi = (rule, value, callback) => {
      if (value.length !== 4) {
        callback(new Error('验证码格式不正确'))
      } else {
        callback()
      }
    }
    return {
      loginForm: {
        loginName: 'admin',
        password: 'admin',
        code: '',
        clientToken: 'SPl5tPeiWaSzcFML2jV31MXlTtK1syQV',
        loginType: 0
      },
      loginRules: {
        loginName: [
          { required: true, message: '请输入账号', trigger: 'blur' }
        ],
        password: [{ required: true, message: '请输入密码', trigger: 'blur' }],
        code: [{ required: true, trigger: 'blur', validator: validateLi }]
      },
      loading: false,
      passwordType: 'password',
      redirect: undefined,
      photoUrl: imgSrc
    }
  },
  methods: {
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },
    handleLogin() {
      this.$refs.loginForm.validate(async(valid) => {
        if (valid) {
          try {
            this.loading = true
            await this.$store.dispatch('user/loginAction', this.loginForm)
            this.$router.push('/dashboard')
          } finally {
            this.loading = false
          }
        }
      })
    },
    async validateLogin() {
      this.loginForm.clientToken = this.loginForm.clientToken === 'cwsbQBCtBp5lYLDuzqzRqiFoSIM2Djpq' ? 'SPl5tPeiWaSzcFML2jV31MXlTtK1syQV' : 'cwsbQBCtBp5lYLDuzqzRqiFoSIM2Djpq'
    }
  }
}
</script>

<style lang="scss">
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

$bg:#fff;
$light_gray:#999;
$cursor: #999;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: $cursor;
  }
}

/* reset element-ui css */
.login-container {
  .el-image__inner {
    position: relative;
    width: 96px;
    height: 96px;
    top: -46px;
    left: 50%;
    margin-left: -48px;
  }
  .img .el-image__inner{
    width: 130px;
    height: 52px;
    position: unset;
    margin: unset;
    vertical-align: middle;
  }
  .el-image {
    position: unset;
    display:unset;
    overflow:unset;
  }
  .validate {
      width: 315px;
    }
  .el-input {
    display: inline-block;
    height: 47px;
    width: 85%;
    input {
      background: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      color: $light_gray;
      height: 47px;
      caret-color: $cursor;

      &:-webkit-autofill {
        box-shadow: 0 0 0px 1000px $bg inset !important;
        -webkit-text-fill-color: $cursor !important;
      }
    }
  }

  .el-form-item {
    border: 1px solid #ddd;
    border-radius: 5px;
    color: #999;
  }
}
</style>

<style lang="scss" scoped>
$bg:#2d3a4b;
$dark_gray:#889aa4;
$light_gray:#eee;

.login-container {
  background: url(../../assets/微信图片_20221026210324.jpg) no-repeat;
  min-height: 100%;
  width: 100%;
  background-color: $bg;
  overflow: hidden;

  .login-form {
    position: absolute;
    width: 518px;
    height: 388px;
    top: 50%;
    left: 50%;
    margin-top: -194px;
    margin-left: -259px;
    padding: 0 35px;
    background: #fff;
    -webkit-box-shadow: 0 3px 70px 0 rgb(30 111 72 / 35%);
    box-shadow: 0 3px 70px 0 rgb(30 111 72 / 35%);
    border-radius: 10px;
  }

  .tips {
    font-size: 14px;
    color: #fff;
    margin-bottom: 10px;

    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }

  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }

  .tab {
    display: flex;
  }
  .title-container {
    position: relative;
    height: 76px;

    .title {
      font-size: 26px;
      color: $light_gray;
      margin: 0px auto 40px auto;
      text-align: center;
      font-weight: bold;
    }
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }
}
</style>
